package contract;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import common.ClampFactory;

public class ContractDAO {

	private SqlSession session;
	ContractDTO cont;
	
	public ContractDAO() {
		try {
			session = ClampFactory.getFactory().openSession();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public ContractDTO getCont() {
		return cont;
	}
	public void setCont(ContractDTO cont) {
		this.cont = cont;
	}
	
	public ArrayList<ContractDTO> lists(int start, int end){
		ArrayList<ContractDTO> lists = null;
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("ct", cont);
			map.put("start", start);
			map.put("end", end);
			putMap(map);
			lists = (ArrayList<ContractDTO>)session.selectList("contract.lists", map);
			//System.out.println(lists.size()+"/"+cont.getSa_search()+"/"+cont.getSa_no());
		} catch (Exception e) {
			e.printStackTrace();
		}
		session.close();
		return lists;
	}
	
	//총 행의 수 반환
	public int totalRow(){
		int row = 0;
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("ct", cont);
			putMap(map);
			row = (int)session.selectOne("contract.totalRow",map);
			ContractDTO c = (ContractDTO)map.get("ct");
			//System.out.println(map.get("full")+"/"+row+"/"+c.getSa_no()+"/"+c.getSa_nm());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return row;
	}
	
	//검색조건있을시 map에 저장
	public void putMap(Map<String, Object> map){
		
		if(cont.getFull() == null || !cont.getFull().equals("full")){
			map.put("full", "no");
		}else{
			map.put("full", "full");
		}
		
		if(cont.getSa_dpm() == null || cont.getSa_dpm().equals("0")) map.put("dpm", "0");
		else map.put("dpm", "dpm");
		
		if(cont.getSa_gd() == null || cont.getSa_gd().equals("0")) map.put("gd", "0");
		else map.put("gd", "gd");
		
		if(cont.getCs_sd() == null || cont.getCs_sd().equals("0")) map.put("sded", "0");
		else map.put("sded", "sded");
		
		if(cont.getCs_loan() == null || cont.getCs_loan().equals("0")) map.put("loan", "0");
		else map.put("loan", "loan");
		
		if(cont.getCt_ing() == null || cont.getCt_ing().equals("0")) map.put("ing", "0");
		else map.put("ing", "ing");
	}
	
	public ContractDTO view(){
		ContractDTO dto = null;
		try {
			dto = (ContractDTO)session.selectOne("contract.view",cont);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return dto;
	}
	
	public int insert(){
		int rs = 0;
		try {
			rs = session.insert("contract.insert", cont);
			if(rs>0){
				session.commit();
			}else{
				session.rollback();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		session.close();
		return rs;
	}
	
	public int update(){
		int rs = 0;
		try {
			rs = session.insert("contract.update",cont);
			if(rs>0){
				session.commit();
			}
			else{
				session.rollback();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		session.close();
		return rs;
	}
	
	public boolean appUp(HashMap<String, Object> map){
		boolean flag = false;
		try {
			int rs = session.insert("contract.appUp",map);
			if(rs>0){
				flag = true;
				session.commit();
			}
			else{
				flag = false;
				session.rollback();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		session.close();
		return flag;
	}
	
	public boolean appUp(){
		boolean flag = false;
		try {
			int rs = session.insert("contract.appUp",cont);
			if(rs>0){
				flag = true;
				session.commit();
			}
			else{
				flag = false;
				session.rollback();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		session.close();
		return flag;
	}
	
	public String returnCTNO(){
		String ct_no=null;
		ct_no = (String)session.selectOne("contract.returnCTNO", cont);
		//session.close();
		return ct_no;
	}
	
}
